# coding: cp1252 
'''
Created on 26/03/2009

@author: Neper
'''
import os
#from show_stars import ShowStarsNode
from BLL.DAL.DvdShareDC import TbDVD, TbGenero, TbTipoDVD
import BLL.DvdBLL
import BLL.GeneroBLL
import BLL.TipoBLL
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
#from google.appengine.ext import db
from google.appengine.ext.webapp import template



# The main page where the user can login and logout
# MainPage is a subclass of webapp.RequestHandler and overwrites the get method
class MainPage(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        url = users.create_login_url(self.request.uri)        
                    
        if user:
            url = users.create_logout_url(self.request.uri)
# GQL is similar to SQL             
        dvds =BLL.DvdBLL.ListaDVD()
                
        values = {
            'dvds': dvds,
            'numdvds' : dvds.count(),
            'user': user,
            'url': url,
            'erro':'',
        }
        #ShowStarsNode()
        template_file = os.path.join(os.path.dirname(__file__), 'templates','dvd.html')        
        rendered = webapp.template.render(template_file, values)
        self.response.out.write(rendered)        
        #self.response.out.write(template.render('/templates/index.html', values))


class Detalhes(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        url = users.create_login_url(self.request.uri)   
        raw_id = int(self.request.get('id'))                             
        if user:
            url = users.create_logout_url(self.request.uri)            
# GQL is similar to SQL
            
        dvd = TbDVD.get_by_id(raw_id)             
        #, author=users.get_current_user())        
        values = {
            'dvd': dvd,            
            'user': user,
            'url': url,
            'acao': 'Reservar',
            'erro':'',
            'youtube':'',
        }
        template_file = os.path.join(os.path.dirname(__file__), 'templates','viewDVD.html')        
        rendered = webapp.template.render(template_file, values)
        self.response.out.write(rendered)   

class FrmManterParametros(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        url = users.create_login_url(self.request.uri)   
        if user:
            url = users.create_logout_url(self.request.uri)   
        values = {
                  'url':url,
                  'user':user,}        
        template_file = os.path.join(os.path.dirname(__file__),'templates','ManterParametros.html')
        render = webapp.template.render(template_file,values)
        self.response.out.write(render)                

class FrmGenero(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        url = users.create_login_url(self.request.uri)   
        generos = BLL.GeneroBLL.ListaGenero()
        if user:
            url = users.create_logout_url(self.request.uri)   
        values = {
                  'generos':generos,
                  'url':url,
                  'user':user,}        
        template_file = os.path.join(os.path.dirname(__file__),'templates','ManterGenero.html')
        render = webapp.template.render(template_file,values)
        self.response.out.write(render)    
class NewGenero(webapp.RequestHandler):
    def post (self):
        user = users.get_current_user()
        if user:
            genero = TbGenero (
                    dsGenero = self.request.get('dsGenero'),)
            BLL.GeneroBLL.Inserir(genero)       
            self.redirect('/frmGenero')  
            
class FrmTipo(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        url = users.create_login_url(self.request.uri)   
        tipos = BLL.TipoBLL.Lista()
        if user:
            url = users.create_logout_url(self.request.uri)   
        values = {
                  'tipos':tipos,
                  'url':url,
                  'user':user,}        
        template_file = os.path.join(os.path.dirname(__file__),'templates','ManterTipo.html')
        render = webapp.template.render(template_file,values)
        self.response.out.write(render)    
class NewTipo(webapp.RequestHandler):
    def post (self):
        user = users.get_current_user()
        if user:
            tipo = TbTipoDVD (
                    dsTipo = self.request.get('dsTipo'),)
            BLL.GeneroBLL.Inserir(tipo)       
            self.redirect('/frmTipo')  

class FrmCadastrar(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        url = users.create_login_url(self.request.uri)   
        generos = BLL.GeneroBLL.ListaGenero()
        tipos = BLL.TipoBLL.Lista()                                      
        if user:
            url = users.create_logout_url(self.request.uri)   
        values = {
                  'url':url,
                  'user':user,
                  'generos': generos,
                  'tipos':tipos,}
                    
        template_file = os.path.join(os.path.dirname(__file__),'templates','CadastrarDVD.html')
        render = webapp.template.render(template_file,values)
        self.response.out.write(render)

    
            
# This class creates a new Todo item
class New(webapp.RequestHandler):
    def post(self):
        user = users.get_current_user()
        if user:
            testurl = self.request.get('url')
            if not testurl.startswith("http://") and testurl:
                testurl = "http://"+ testurl
            dvd = TbDVD(
                Proprietario  = users.get_current_user(),
                #RfTipoDvd
                #RfGenero
                dsTitulo = self.request.get('dsTitulo'),
                dsAtorPrincipal = self.request.get('dsAtorPrincipal'),
                dsDiretor = self.request.get('dsDiretor'),
                dsSinopse = self.request.get('dsSinopse'),
                dsUrlTraler = self.request.get('dsUrlTraler'),
                nuDuracao = int( self.request.get('nuDuracao')),
                nuNota = int (self.request.get('nuNota')),
                blLocado=False,)
            BLL.DvdBLL.Inserir(dvd)
                                                   
            self.redirect('/')           

# This class deletes the selected Todo
class Done(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        if user:
            raw_id = self.request.get('id')
            id = int(raw_id)
            BLL.DvdBLL.Deletar(id)
            self.redirect('/')

#This class emails the task to yourself
class Email(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        if user:
            raw_id = self.request.get('id')
            id = int(raw_id)
            BLL.DvdBLL.EnviarEmail(id)
            self.redirect('/')

# Register the URL with the responsible classes
application = webapp.WSGIApplication(
                                     [('/', MainPage),
                                      ('/fnew',FrmCadastrar),
                                      ('/fpar',FrmManterParametros),
                                      ('/frmGenero',FrmGenero),
                                      ('/newGenero',NewGenero),
                                      ('/frmTipo',FrmTipo),
                                      ('/newTipo',NewTipo),
                                      ('/new', New),
                                      ('/done', Done),
                                      ('/info', Detalhes),
                                      ('/email', Email)],
                                      debug=True)

# Register the wsgi application to run
def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()